<?
/**
 * VoiceOne Settings/Extensions
 *
 * Frontend to provide access to the extensions system in voiceone.
 * @package voiceone
 * @subpackage  settings_extensions
 */
 
  include ("../../admin/config/cfg.php");
  global $cwlang;
  
  //================ PAGE LOGIC ================
  $alert=getAlert();
  
  if ($_GET["cmd"]=="del") {
    $ret=$asterisk->delClient($_GET["id"]);
    if ($ret==1) $alert="<div class=\"AlertGood\">".$cwlang["exten"]["delSuccessfully"]."</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["exten"]["delFailed"]."</div>";
    goToMainPage($alert,"./");
  }
  
  $ret=$asterisk->getClient(0,0);
  if ($err=$asterisk->getError()) die("Error: $err");


  $count=0;
  if (is_array($ret)) {
    foreach($ret as $v) {
      $id=$v["id"];
      $ext=$v["extension"];
      $tech=$v["technology"];
      $callerid=$v["name"]." ".$v["surname"];
      $icon=getIcon($tech);
      $html.="<td>$icon  <a href=\"edit/?id=$id\">$callerid &lt;$ext&gt;</a>  <a href=\"$PHP_SELF?cmd=del&id=$id\" onclick=\"if (confirm('".$cwlang["exten"]["confirmDel"]." $ext?')) return true; else return false;\"><img src=\"$relBasePath/public/img/trash.gif\" /></a></td>\n";
      if ((++$count)%2==0) $html.="</tr><tr>\n";
    }
  }  

  //prepare the sidebar
  $retBuddy=$asterisk->getClient(0,0);
  if ($err=$asterisk->getError()) die("Error: $err");
  if (is_array($retBuddy))
  foreach ($retBuddy as $vBuddy) {
    $buddyid=$vBuddy["id"];
    $buddyExt=$vBuddy["extension"];
    $buddyName=$vBuddy["name"];
    $buddySurname=$vBuddy["surname"];
    $buddiesList.="<tr><td class=\"postit\">$buddyExt</td><td><a href=\"./edit/?id=$buddyid\">$buddyName $buddySurname</a></td></tr>\n";
  }  
  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=$cwlang["exten"]["title"]?></h1>
    <h3>
      <a href="edit/" class="admin" ><?=$cwlang["exten"]["newExten"]?></a> 
    </h3>
  </div>
  <table class="todolist">
    <tr>
      <?=$html?>
    </tr>
  </table>
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
        <div class="Sidebar">
          <h1><?=$cwlang["exten"]["information"]?></h1>
          <table class="small">
            <?=$buddiesList?>
          </table>
          <p>
            <img src="../../admin/public/img/grandstream.jpg" alt="Grand Stream" />
          </p>
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================

  $smarty->assign("section", "extensions");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('settings.tpl');
?>

